#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename:   01-packages-master.sh
# Version:    1.0
# Date:       2020/04/23
# Author:     Lz
# Email:      lz843723683@163.com
# History：     
#             Version 1.0, 2020/04/22
# Function:   packages - 01.测试金风主控版本包列表需求
# Out:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

## TODO : 测试收尾清除工作
#
PkgClean_FTGW_MASTER(){
	return $TPASS
}


## TODO: 调用程序退出函数
#    In: $1 => 调用脚本结果值
PkgExit_FTGW_MASTER(){
        # 调用退出函数，其中调用了clean相关函数
	Exit_FTLIB ${1}
}


## TODO: 用户界面
#
PkgUSAGE_FTGW_MASTER(){
	USAGE_FTLIB "金风主控版本 - 包列表需求"
}


## TODO : 测试前的初始化 
#  Out  : 
#         0 => TPASS
#         1 => TFAIL
#         other=> TCONF
PkgInit_FTGW_MASTER(){
        # 调用初始化函数
	Init_FTLIB "PkgClean_FTGW_MASTER"
        # 调用用户界面函数
 	PkgUSAGE_FTGW_MASTER
}


## TODO ：默认集成mono，并且版本为5.4
#
PkgTest01_FTGW_MASTER(){
	Mono_FTGENREQ
}


## TODO ：1. 默认系统自带9.6版本Postgresql数据库
#
PkgTest02_FTGW_MASTER(){
	local pkgarr="postgresql postgresql-server"
	
	# 默认安装pkgarr
	PkgExistArr_FTLIB "isExist" "no" "yes" "${pkgarr}"
	RetParse_FTLIB "要求默认安装软件包：$pkgarr" "False"

	# 要求版本为9.6
	local cmd="psql"

	VersionEQTest_FTLIB "$cmd" "--version" "9.6"
}


## TODO ：默认未安装monodevelop
#
PkgTest03_FTGW_MASTER(){
	local pkgname="monodevelop"

	# 默认未安装monodevelop
	PkgExist_FTLIB "noExist" "no" "yes" "${pkgname}"
	RetParse_FTLIB "要求不安装软件包 ${pkgname}" "False"
}


## TODO ：默认安装Redis，并且开机自启动
#
PkgTest04_FTGW_MASTER(){
	local pkgname="redis"
	local svcname="${pkgname}"

	# 默认安装Redis
	PkgExist_FTLIB "isExist" "no" "yes" "${pkgname}"
	RetParse_FTLIB "默认安装${pkgname}相关包" "False"
	
	# 判断服务是否激活
	SvcActive_FTLIB "isActive" "no" "${svcname}"
	RetParse_FTLIB "已启动${svcname}服务" "False"

	# 开机自启动
	SvcEnable_FTLIB "isEnable" "no" "${svcname}"
	RetParse_FTLIB "开机自启动${svcname}服务" "False"
}


## TODO ：默认安装nginx，并且服务能够正常启动
#
PkgTest05_FTGW_MASTER(){
	local pkgname="nginx"
	local svcname="${pkgname}"
	local ret="${TPASS}"

	# 默认安装nginx
	PkgExist_FTLIB "isExist" "no" "yes" "${pkgname}"
	RetParse_FTLIB "默认安装${pkgname}相关包" "False"

	# 判断是否存在nginx
	SvcExist_FTLIB "isExist" "no" "${svcname}"
	RetParse_FTLIB "存在${svcname}服务" "False"

	# 判断nginx是否默认打开
	SvcActive_FTLIB "isActive" "yes" "${svcname}"	
	if [ $? -eq 0 ];then
	# 已经nginx打开
		SvcActive_FTLIB "isActive" "no" "${svcname}"	
		RetParse_FTLIB "已启动${svcname}服务" "False"
	else
	# 当前nginx处于关闭状态
		# 开启服务
                which systemctl &>/dev/null
                if [ $? -eq 0 ];then
                        systemctl start ${svcname}
                else
                        service ${svcname} start
                fi

		# 判断服务是否成功打开
		SvcActive_FTLIB "isActive" "no" "${svcname}"	
		ret="$?"

		# 关闭服务
                which systemctl &>/dev/null
                if [ $? -eq 0 ];then
                        systemctl stop ${svcname}
                else
                        service ${svcname} stop
                fi

		OutputRet_FTLIB "$ret"
		RetParse_FTLIB "可以成功启动${svcname}服务" "False"
	fi
}


## TODO ：默认未安装wine
#
PkgTest06_FTGW_MASTER(){
	local pkgname="wine"

	# 默认未安装wine
	PkgExist_FTLIB "noExist" "no" "yes" "${pkgname}"
	RetParse_FTLIB "要求不安装软件包 ${pkgname} " "False"
}


## TODO ：1. 默认安装kylin-apache-tomcat
#         2. 默认不自启动tomcat
#
PkgTest07_FTGW_MASTER(){
	# 1. 默认安装kylin-apache-tomcat
	local pkgname="kylin-apache-tomcat"
	PkgExist_FTLIB "isExist" "no" "yes" "${pkgname}"
	RetParse_FTLIB "集成${pkgname}包" "False"

	# 2. 默认不自启动tomcat
	local svcname="tomcat"
	SvcActive_FTLIB "noActive" "no" "${svcname}"
	RetParse_FTLIB "未启动${svcname}服务" "False"

	SvcEnable_FTLIB "noEnable" "no" "${svcname}"
	RetParse_FTLIB "开机未自启动${svcname}服务" "False"
}


## TODO ：默认安装python3.6
#
PkgTest08_FTGW_MASTER(){
	# 默认安装python36
	local pkgname="python3"
	PkgExist_FTLIB "isExist" "no" "no" "${pkgname}"
	RetParse_FTLIB "集成${pkgname}包" "False"

	# 要求python版本为3.6
	local cmd="python36"

	VersionEQTest_FTLIB "$cmd" "--version" "3.6"
}


## TODO ：默认安装软件包 gcc,gcc-c++,compat-libstdc++,Chromium,thrift、zabbix、zeromq、HDF5、sqlite、lrzsz、Mplayer
#
PkgTest09_FTGW_MASTER(){
	local pkgArr="gcc gcc-c++ compat-libstdc++ mplayer chromium thrift hdf5 sqlite lrzsz"

	PkgExistArr_FTLIB "isExist" "no" "yes" "$pkgArr"
	RetParse_FTLIB "要求默认软件包: $pkgArr" "False"

	pkgArr="zabbix zeromq"
	PkgExistArr_FTLIB "isExist" "no" "no" "$pkgArr"
	RetParse_FTLIB "要求默认软件相关包: $pkgArr" "False"

        # Chromium升级到79版本，默认56版本不能播报语音
        local cmd="chromium-browser"
        VersionEQTest_FTLIB "$cmd" "--version" "Chromium 79"
}


## TODO ：docker版本为docker 19.03.8-ce
#
PkgTest10_FTGW_MASTER(){
	# 默认安装 docker-ce
	local pkgname="docker-ce"
	PkgExist_FTLIB "isExist" "no" "yes" "${pkgname}"

	RetParse_FTLIB "集成 ${pkgname} 包" "False"

	# 测试版本
	local cmd="docker"

	VersionEQTest_FTLIB "$cmd" "--version" "19.03.8"
}


## TODO ：默认安装go语言，并且版本为1.9
#
PkgTest11_FTGW_MASTER(){
	# 测试版本
	local cmd="go"

	VersionEQTest_FTLIB "$cmd" "version" "go1.9"
}


## TODO ：jdk版本默认使用1.8.0_162
#
PkgTest12_FTGW_MASTER(){
	JavaVersion_FTGENREQ
}


## TODO ：pango需要从1.42.1降级到1.40.4
#
PkgTest13_FTGW_MASTER(){
	local pkgname="pango"

	PkgExist_FTLIB "isExist" "no" "yes" "${pkgname}"
        RetParse_FTLIB "集成${pkgname}相关包" "False"

	# 测试版本
	local cmd="pango-view"

	VersionEQTest_FTLIB "$cmd" "--version" "1.40.4"
}


## TODO ：默认kdump默认自启，并且运行正常
#
PkgTest14_FTGW_MASTER(){
        #  默认自启动kdump
        local svcname="kdump"

        # 判断是否存在svcname
        SvcExist_FTLIB "isExist" "no" "${svcname}"
        RetParse_FTLIB "存在 ${svcname} 服务" "False"

        # 判断 svcname 是否默认自启动
        SvcEnable_FTLIB "isEnable" "no" "${svcname}"
        RetParse_FTLIB "自启动 ${svcname} 服务" "False"

        # 判断 svcname 是否已经打开
        SvcActive_FTLIB "isActive" "no" "${svcname}"
        RetParse_FTLIB "已启动${svcname}服务" "False"
}


## TODO ：1. gd_Kylin_agent 服务必须存在
#         2. gd_kylin_agent 默认不能自启动
#
PkgTest15_FTGW_MASTER(){
        local svcname="gd_kylin_agent"

        # 判断是否存在svcname
        SvcExist_FTLIB "isExist" "no" "${svcname}"
        RetParse_FTLIB "存在 ${svcname} 服务" "False"

        # 判断 svcname 是否默认自启动
        SvcEnable_FTLIB "noEnable" "no" "${svcname}"
        RetParse_FTLIB "要求不自启动 ${svcname} 服务" "False"
}


## TODO : Main
#  Out  : 
#         0 => TPASS
#         1 => TFAIL
#         other => TCONF
PkgMain_FTGW_MASTER(){
	PkgInit_FTGW_MASTER

	PkgTest01_FTGW_MASTER

	PkgTest02_FTGW_MASTER

	PkgTest03_FTGW_MASTER

	PkgTest04_FTGW_MASTER

	PkgTest05_FTGW_MASTER

	PkgTest06_FTGW_MASTER

	PkgTest07_FTGW_MASTER

	PkgTest08_FTGW_MASTER

	PkgTest09_FTGW_MASTER

	PkgTest10_FTGW_MASTER

	PkgTest11_FTGW_MASTER

	PkgTest12_FTGW_MASTER

	PkgTest13_FTGW_MASTER

	PkgTest14_FTGW_MASTER

	PkgTest15_FTGW_MASTER

	return ${TPASS}	
}

PkgMain_FTGW_MASTER
PkgExit_FTGW_MASTER $?
